package by.grsu.eo.pages;

import java.util.List;

import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;

import by.grsu.eo.dao.UserDao;
import by.grsu.eo.page.MainWebPage;
import by.grsu.eo.payment.system.User;


public class ListUsers extends MainWebPage {
	
	@SpringBean
	private UserDao userDao;

	public ListUsers() {
		super();
		// retrieve all objects
		final List<User> users = userDao.findAll();

		add(new ListView<User>("users", users) {

			@Override
			protected void populateItem(final ListItem<User> item) {
				final User user = item.getModelObject();
				// add "labels" to display each property of the item
				item.add(new Label("firstName", user.getFirstName()));
				item.add(new Label("lastName", user.getLastName()));
				item.add(new Label("login", user.getLogin()));
				item.add(new Label("password", user.getPassword()));
				//item.add(new Label("role", user.getRole().toString()));

				// add link which will forward to the "edit" page
				item.add(new Link<User>("editUser", new Model<User>(user)) {
					@Override
					public void onClick() {
						setResponsePage(new EditUser(user));
					}
				});

				// add "link" which removes item from database and reload the
				// page
				item.add(new Link<User>("deleteUser", new Model<User>(user)) {
					@Override
					public void onClick() {
						userDao.delete(getModelObject().getId());
						setResponsePage(ListUsers.class);
					}
				});
			}
		});
		
		add(new Link<User>("createUser", new Model<User>(new User())) {
			@Override
			public void onClick() {
				setResponsePage(new EditUser(getModelObject()));
			}
		});

	}

}
